import { defineStore } from 'pinia'
import { ref } from 'vue'
import { shopApi } from '@/api'

export const useShopStore = defineStore('shop', () => {
  const options = ref<any[]>([])

  const fetchOptions = async () => {
    if (options.value.length > 0) {
      return options.value
    } else {
      options.value = await getProcessedData()
      return options.value
    }
  }
  const getProcessedData = async () => {
    try {
      const {
        data: { list },
      } = await shopApi.getShopList({ is_all: true })

      const processedData = list.map((item: any) => ({
        label: item.name + '(' + item.code + ')',
        value: String(item.code),
        key: item.code,
      }))
      processedData.unshift({
        label: '全部',
        value: '',
        key: '',
      })
      return processedData
    } catch (error) {
      console.error('獲取全部店鋪數據失敗:', error)
    }
  }
  return {
    options,
    fetchOptions,
  }
})
